package cn.edu.pzhu.servlet;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import cn.edu.pzhu.pojo.Msg;
import cn.edu.pzhu.pojo.UserInfo;
import cn.edu.pzhu.service.UserInfoService;
import cn.edu.pzhu.service.imp.UserInfoServiceImp;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/userinfo")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		//1.接收参数
		String username=request.getParameter("username");
		String email=request.getParameter("email");
		String sex=request.getParameter("sex");
		String[] types=request.getParameterValues("type");
		
		HttpSession session =  request.getSession();
	    
		//2.数据校验	
		if (email == null || email.isEmpty()) {
			// 邮箱格式不正确
			session.setAttribute("msg", "邮箱不能为空！");
			session.setAttribute("msg1", "个人信息页面");
			session.setAttribute("url", "userinfo.jsp");
			// 返回视图View(异常处理建议使用重定向)
			response.sendRedirect("error.jsp");
			return; // 流程结束，后续代码不需要再执行...
		}	
		String reg="^[a-zA-Z0-9_+&*-]+@[a-zA-Z0-9-]+\\.[a-zA-Z]{1,3}$";
		if (!Pattern.matches(reg, email)) {
			// 邮箱格式不正确
			session.setAttribute("msg", "邮箱格式不正确，请按照(x@x.x)的格式重新填写！");
			session.setAttribute("msg1", "个人信息页面");
			session.setAttribute("url", "userinfo.jsp");
			// 返回视图View(异常处理建议使用重定向)
			response.sendRedirect("error.jsp");
			return; // 流程结束，后续代码不需要再执行...
		}
		
		int gender = Integer.parseInt(sex);
		if(types==null) { //复选框没有选择，结果是null
			//提醒关注类型不能为空			
			session.setAttribute("msg", "关注类型不能为空");	
			session.setAttribute("msg1", "个人信息页面");
			session.setAttribute("url", "userinfo.jsp");
			//返回视图View(异常处理建议使用重定向)
			response.sendRedirect("error.jsp");
			return; //流程结束，后续代码不需要再执行...
		}
				
		String type = JSON.toJSONString(types); //类型转换
					
		//3.数据封装	
		UserInfo userinfo = new UserInfo(username, email, gender , type);
		
		//4.调用业务层进行业务处理
		UserInfoService uis = new UserInfoServiceImp();
		
		Msg msg = uis.modUserInfo(userinfo);//执行修改
		
		//5.保存信息回到视图
		if (msg.isSuccess()) {
			// 修改成功后，重新获取用户信息
			UserInfo new_userinfo = uis.showUserInfo(username);
			session.setAttribute("userinfo", new_userinfo);
			String dbtype = new_userinfo.getType();
			List<String> new_types = JSON.parseArray(dbtype, String.class);
			session.setAttribute("types", new_types);
		}
		
		//6.回到个人信息页面
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print("<script>alert('修改成功!');location.href='userinfo.jsp'</script>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
